<?php
/*
	新版系统支付接口
	新版支付接口的微信支付、支付宝支付等有异步结果回调通知的支付方式，此处支付完成后不再更改支付状态，待收到异步支付结果通知后再修改支付状态。
	新增付款码支付方式，付款码支付获取订单支付进行，进入支付页面，发起支付请求，完成支付，提示支付成功！然后呢？
	
	
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'pay';
$return=array();
$setting=uni_setting('pay');
set_html('title','支付页面');
if($_RQ['op']=='copy'){
	if($_SESSION['container']=='wechat'){
		$id=intval($_RQ['id']);
		$pay_url=custom_url('core/member.pay/copy/id='.$id,true);
		$return['pay_url']=$pay_url;
		exi($return);
	}else{
		$_RQ['op']='pay';
	}	
}
if($_RQ['op']=='pay'){	
	$id=intval($_RQ['id']);
	if(!$id){
		exi('发起支付失败！请先选择要支付的订单！','error');
	}
	$pay=pdo_get('core_paylog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));	
	if(!$pay){
		exi('找不到要支付的订单！','error');
	}elseif($pay['no_type']){
		$pay['no_type']=json_decode($pay['no_type'],true);
	}
	if($pay['status']){
		exi('订单已支付！','error');
	}
	$pay_types=array('hdfk','yinlian','alipay','wechat');//vip8pay
	if($_SESSION['uid']){
		$pay_types[]='credit';
	}
	/*
	if($_SESSION['container']!='wechat'){
		//不在微信环境，添加支付宝支付
		$pay_types[]='alipay';
	}
	if($_SESSION['container']!='alipay'){
		//不在支付宝环境，添加微信支付
		$pay_types[]='wechat';
	}*/
	foreach($pay_types as $key=>$type){
		if($setting[$type]!=1){
			unset($pay_types[$key]);
		}
	}
	if($pay['no_type']){
		$pay_types=array_diff($pay_types,$pay['no_type']);
	}
	if($_RQ['pay_type']=='alipay'){
		$pay_types=array('alipay');
	}
	$return=array(
		'id'=>$pay['id'],
		'tid'=>$pay['tid'],
		'fee'=>$pay['fee'],
		'pay_types'=>array_values($pay_types),
		//'setting'=>$setting,
	);
	if($_SESSION['uid']){
		$return['mid']=$_SESSION['uid'];
		$member=pdo_get('core_members',array('id'=>$_SESSION['uid']),array('credit2','credit3','credit4','credit5','credit6'));
		$return['credit']=$member['credit2'];
		$creditdata=model('core/member')->creditdata($member);
		$return['creditdata']=$creditdata;
	}
	if($pay['pay_type'] && in_array($pay['pay_type'],$return['pay_types'])){
		$return['pay_type']=$pay['pay_type'];
	}
	if(!$return['pay_types'] && !$return['pay_type']){
		exi('没有可用的支付方式，请联系系统管理员！','error');
	}
	if(count($return['pay_types'])==1){
		$return['pay_type']=$return['pay_types']['0'];
	}
	if($_RQ['forward_url']){
		$return['forward_url']=base64_decode($_RQ['forward_url']);
	}
	exi($return);
}
if($_RQ['op']=='alipay'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$pay_log=pdo_get('core_paylog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if($pay_log['fee']>0){
		$params=array(
			'title'=>$pay_log['title'],
			'fee'=>$pay_log['fee'],
			'ordersn'=>$pay_log['tid'],
			'return_url'=>custom_url($pay_log['success_url'],true),
		);
		$pay_html=model('jy_alipay/alipay')->mobile_pay($params);
		exi(base64_encode($pay_html));
	}else{
		pdo_update('core_paylog',array('pay_type'=>'alipay','status'=>1),array('id'=>$id));
		//此处模拟微信支付等进行异步支付结果通知~
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'uid'=>$_SESSION['uid'],
			'timestamp'=>TIMESTAMP,
			'back_url'=>$pay_log['back_url'],
			'pay_result'=>ijson_encode(array('id'=>$pay_log['oid'],'status'=>1,'pay_type'=>'credit','fee'=>$pay_log['fee'],'pay_id'=>$pay_log['id'],'pay_tid'=>$pay_log['tid'],'title'=>$pay_log['title'])),
		);
		$data['sign']=get_sign($data);
		$res=cfc('http')->ihttp_request(SITEROOT.'custom/payresult/credit_notify.php',ijson_encode($data));
		exi('success');
	}
}
if($_RQ['op']=='wechat'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$pay_log=pdo_get('core_paylog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if($pay_log['fee']>0){
		$params=array(
			'title'=>$pay_log['title'],
			'fee'=>$pay_log['fee'],
			'tid'=>$pay_log['tid'],
		);
		if($setting['wechat']==1){
			$wechat=$setting;
		}else{
			//借用系统支付
			exi('该公众号暂不支持微信支付！','error');
			//获取粉丝openid
			
		}
		if(!$wechat['wechat_mchid'] || !$wechat['wechat_miyue']){
			exi('支付出错：微信支付参数设置错误！','error');
		}
		if($_SESSION['container']=='wechat'){
			$wechat_options=cfc('wechat_pay')->jsapi_pay($params);
			exi($wechat_options);
		}else{
			//非微信环境，发起手机网页支付
			$murl=cfc('wechat_pay')->h5_pay($params);
			exi($murl);
		}
	}else{
		pdo_update('core_paylog',array('pay_type'=>'wechat','status'=>1),array('id'=>$id));
		//此处模拟微信支付等进行异步支付结果通知~
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'uid'=>$_SESSION['uid'],
			'timestamp'=>TIMESTAMP,
			'back_url'=>$pay_log['back_url'],
			'pay_result'=>ijson_encode(array('id'=>$pay_log['oid'],'status'=>1,'pay_type'=>'credit','fee'=>$pay_log['fee'],'pay_id'=>$pay_log['id'],'pay_tid'=>$pay_log['tid'],'title'=>$pay_log['title'])),
		);
		$data['sign']=get_sign($data);
		$res=cfc('http')->ihttp_request(SITEROOT.'custom/payresult/credit_notify.php',ijson_encode($data));
		exi('success');
	}
	
}
if($_RQ['op']=='wxapp_pay'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$pay_log=pdo_get('core_paylog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if($pay_log['fee']>0){
		$params=array(
			'title'=>$pay_log['title'],
			'fee'=>$pay_log['fee'],
			'tid'=>$pay_log['tid'],
			'success_url'=>$pay_log['success_url'],
			'error_url'=>$pay_log['error_url'],
		);
		if($params['fee']==0){
			exi('订单金额不能为零！','error');
		}
		$wxapp=pdo_get('core_account_wxapp',array('id'=>$_SESSION['appgid'],'uniacid'=>$_SESSION['uniacid']),array('appid','mchid','paykey'));
		if(!$wxapp || !$wxapp['appid'] || !$wxapp['mchid'] || !$wxapp['paykey']){
			exi('发起支付失败！小程序支付参数设置错误！','error');
		}
		$setting=array(
			'appid'=>$wxapp['appid'],
			'mch_id'=>$wxapp['mchid'],
			'key'=>$wxapp['paykey'],
		);	
		$res=cfc('wechat_pay',$setting)->wxapp_pay($params);
		exi($res);
	}else{
		pdo_update('core_paylog',array('pay_type'=>'wechat','status'=>1),array('id'=>$id));
		//此处模拟微信支付等进行异步支付结果通知~
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'uid'=>$_SESSION['uid'],
			'timestamp'=>TIMESTAMP,
			'back_url'=>$pay_log['back_url'],
			'pay_result'=>ijson_encode(array('id'=>$pay_log['oid'],'status'=>1,'pay_type'=>'credit','fee'=>$pay_log['fee'],'pay_id'=>$pay_log['id'],'pay_tid'=>$pay_log['tid'],'title'=>$pay_log['title'])),
		);
		$data['sign']=get_sign($data);
		$res=cfc('http')->ihttp_request(SITEROOT.'custom/payresult/credit_notify.php',ijson_encode($data));
		exi('success');
	}
}
if($_RQ['op']=='aliapp_pay'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$pay_log=pdo_get('core_paylog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if($pay_log['fee']>0){
		$params=array(
			'title'=>$pay_log['title'],
			'fee'=>$pay_log['fee'],
			'ordersn'=>$pay_log['tid'],
			'success_url'=>$pay_log['success_url'],
			'error_url'=>$pay_log['error_url'],
		);
		$wxapp=pdo_get('core_account_wxapp',array('id'=>$_SESSION['aliapp_id'],'uniacid'=>$_SESSION['uniacid']));
		$setting=array(
			'alipay_appid'=>$wxapp['ali_appid'],
			'alipay_private_key'=>$wxapp['alipay_private_key'],
			'alipay_public_key'=>$wxapp['alipay_public_key'],
		);
		$return['trade_no']=model('jy_alipay/alipay',$setting)->aliapp_pay($params);
		exi($return);
	}else{
		pdo_update('core_paylog',array('pay_type'=>'alipay','status'=>1),array('id'=>$id));
		//此处模拟微信支付等进行异步支付结果通知~
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'uid'=>$_SESSION['uid'],
			'timestamp'=>TIMESTAMP,
			'back_url'=>$pay_log['back_url'],
			'pay_result'=>ijson_encode(array('id'=>$pay_log['oid'],'status'=>1,'pay_type'=>'credit','fee'=>$pay_log['fee'],'pay_id'=>$pay_log['id'],'pay_tid'=>$pay_log['tid'],'title'=>$pay_log['title'])),
		);
		$data['sign']=get_sign($data);
		$res=cfc('http')->ihttp_request(SITEROOT.'custom/payresult/credit_notify.php',ijson_encode($data));
		exi('success');
	}
}
if($_RQ['op']=='credit'){
	$id=intval($_RQ['id']);
	$type=$_RQ['type'];
	if(!$id){
		exi('参数错误！','error');
	}
	$creditfield=(!$type || $type=='credit')?'credit2':$type;
	$pay_log=pdo_get('core_paylog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$creditnum=pdo_getcolumn('core_members',array('id'=>$_SESSION['uid']),$creditfield);
	$key=substr($creditfield, -1);
	$typetitle=credit_types()[$key];
	$credit_rate=$setting['credit'.$key.'_rate']?:1;
	if($pay_log['fee']*$credit_rate>$creditnum){
		exi($typetitle.'不足！当前账户['.$typetitle.']：'.$creditnum.','.$credit_rate.$typetitle.'=1元','error');
	}
	$creditnum=$creditnum-$pay_log['fee'];
	pdo_update('core_members',array($creditfield=>$creditnum),array('id'=>$_SESSION['uid']));
	pdo_update('core_paylog',array('pay_type'=>$type,'status'=>1),array('id'=>$id));
	$log=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$_SESSION['uid'],
		'type'=>$key,
		'is_add'=>0,
		'fee'=>$pay_log['fee'],
		//'result'=>$credit2,
		'remark'=>'支付订单：'.$pay_log['title'],
		'createtime'=>TIMESTAMP
	);
	pdo_insert('core_credit_log',$log);
	//此处模拟微信支付等进行异步支付结果通知~
	$data=array(
		'uniacid'=>$_SESSION['uniacid'],
		'uid'=>$_SESSION['uid'],
		'timestamp'=>TIMESTAMP,
		'back_url'=>$pay_log['back_url'],
		'pay_result'=>ijson_encode(array('id'=>$pay_log['oid'],'status'=>1,'pay_type'=>$type,'fee'=>$pay_log['fee'],'pay_id'=>$pay_log['id'],'pay_tid'=>$pay_log['tid'],'title'=>$pay_log['title'])),
	);
	$data['sign']=get_sign($data);
	$res=cfc('http')->ihttp_request(SITEROOT.'custom/payresult/credit_notify.php',ijson_encode($data));
	exi('success');
}
if($_RQ['op']=='hdfk'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$pay_log=pdo_get('core_paylog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	pdo_update('core_paylog',array('pay_type'=>'hdfk','status'=>0),array('id'=>$id));
	//此处模拟微信支付等进行异步支付结果通知~
	$data=array(
		'uniacid'=>$_SESSION['uniacid'],
		'uid'=>$_SESSION['uid'],
		'timestamp'=>TIMESTAMP,
		'back_url'=>$pay_log['back_url'],
		'pay_result'=>ijson_encode(array('id'=>$pay_log['oid'],'status'=>0,'pay_type'=>'hdfk','fee'=>$pay_log['fee'],'pay_id'=>$pay_log['id'],'pay_tid'=>$pay_log['tid'],'title'=>$pay_log['title'])),
	);
	$data['sign']=get_sign($data);
	$res=cfc('http')->ihttp_request(SITEROOT.'custom/payresult/credit_notify.php',ijson_encode($data));
	exi('success');
}
if($_RQ['op']=='yinlian'){
	
}
if($_RQ['op']=='vip8pay'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$pay_log=pdo_get('core_paylog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if($pay_log['fee']>0){
		if($setting['vip8pay']==1){
			$vip8pay=$setting;
		}else{
			//借用系统支付
			exi('该公众号暂不支持快捷支付！','error');
			//获取粉丝openid	
		}
		if(!$vip8pay['vip8_shopid'] || !$vip8pay['vip8_uniacid'] || !$vip8pay['vip8_groupid']){
			exi('支付出错：快捷支付参数设置不全！','error');
		}
		$vip8_options=array(
			'shopid'=>$vip8pay['vip8_shopid'],
			'uniacid'=>$vip8pay['vip8_uniacid'],
			'groupid'=>$vip8pay['vip8_groupid'],
			'openid'=>$_SESSION['openid'],
			'mch_order_num'=>$pay_log['tid'],
			'type'=>2,
			'total_fee'=>$pay_log['fee'],
			'notify_url'=>SITEROOT.'/custom/payresult/vip8pay_notify.php',
			'source_id'=>0,
		);
		if($_SESSION['container']=='wechat'){		
			$vip8_options['pay_type']='wechat_official_account';
		}else{
			//非微信环境，发起手机网页支付	
			$vip8_options['pay_type']='wechat_official_account';
		}
		$res=cfc('pay')->vip8pay_build($vip8_options);
		exi($res);
	}else{
		pdo_update('core_paylog',array('pay_type'=>'wechat','status'=>1),array('id'=>$id));
		//此处模拟微信支付等进行异步支付结果通知~
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'uid'=>$_SESSION['uid'],
			'timestamp'=>TIMESTAMP,
			'back_url'=>$pay_log['back_url'],
			'pay_result'=>ijson_encode(array('id'=>$pay_log['oid'],'status'=>1,'pay_type'=>'credit','fee'=>$pay_log['fee'],'pay_id'=>$pay_log['id'],'pay_tid'=>$pay_log['tid'],'title'=>$pay_log['title'])),
		);
		$data['sign']=get_sign($data);
		$res=cfc('http')->ihttp_request(SITEROOT.'custom/payresult/credit_notify.php',ijson_encode($data));
		exi('success');
	}
	
}
if($_RQ['op']=='wxapp_result'){
	$id=intval($_RQ['orderid']);
	$openid=trim($_RQ['openid']);
	if($_RQ['module_name'] == 'ewei_shopv2'){
		$_RQ['m']='ewei_shopv2';
		$_RQ['r']='app.order.pay.complete';
		$_RQ['do']='mobile';
		$_RQ['id']=$id;
		require(ROOT_D.'/api/open_we7/index.php');
		exit;
	}
}
